﻿@{
    ViewData["Title"] = "MeasureUnitIndex";
    Layout = null;
}


<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />

@* 查询条件 *@
<form class="layui-form layui-row layui-col-space16">
    <div class="layui-col-sm3">
        <input type="text" name="A" id="unitCode" placeholder="单位编码" class="layui-input">
    </div>
    <div class="layui-col-sm3">
        <div class="layui-input-wrap">
            <input type="text" name="B" id="unitName" placeholder="单位名称" lay-affix="clear" class="layui-input">
        </div>
    </div>
    <button type="button" class="layui-btn layui-bg-blue" id="searchButton">查询</button>
    <button type="button" class="layui-btn" onclick="location.reload()">重置</button>
    <button type="button" class="layui-btn layui-bg-purple" onclick="addInfor()">添加</button>
</form>


@* 显示数据表格 *@
<table class="layui-hide" id="mainData" lay-filter="mainData" style="margin-top: -10px;margin-left: 20px">
</table>

<script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="upd">修改</a>
    <a class="layui-btn layui-btn-orange layui-btn-xs" lay-event="delete">删除</a>
</script>

<script src="~/libs/layui-v2.5.6/layui/layui.all.js"></script>
<script src="~/libs/jquery/jquery.js"></script>

<script>


     layui.use('table', function () {
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;

           function showData(){
              table.render({
                 elem: '#mainData',
                 url: '/MainData/MeasureUnitShow', // 实际接口地址
                 page: true, // 开启分页
                 limit: 10, // 每页默认显示的条数
                 limits: [10, 20, 30], // 每页条数的选择项
                 cols: [
                             [
                             { field: 'id', title: '编号', width: 130 },
                             { field: 'unitCode', title: '单位编码', width: 130 },
                             { field: 'unitName', title: '单位名称', width: 130 },
                             {
                                 field: 'isNoUnit',
                                 title: '是否是主单位',
                                 width: 130,
                                 templet: function (d) {
                                     return d.isNoUnit===1 ? '是' : '否';
                                 }
                             },
                             { field: 'conversion', title: '与主单位换算比例', width: 130 },
                             {
                            field: 'isUsed',
                            title: '是否启用',
                            templet: function (d) {
                                return d.isUsed===1 ? '是' : '否';
                            }
                             },
                             { field: 'remark', title: '备注' },
                             { title: '操作', fixed: 'right', width: 300, align: 'center', toolbar: '#barDemo' }
                             ]
                 ],
                 where: {
                     unitCode: $("#unitCode").val(), //获取输入框的单位代码
                     unitName: $("#unitName").val()  //获取输入框的单位名称
                 },
                 parseData: function (res) {
                 console.log(res);
                     return {
                          code: res.code,
                          count: res.data.currentPage,
                          data: res.data.queryable
                      };
                 },
              })
           }

            showData()

           //查询
           $("#searchButton").on("click", function() {
               console.log("查询");
               showData();
           });

          // 触发单元格事件
          //删除
           table.on('tool(mainData)', function(obj) {
              var data = obj.data;//获取当前行数据
              if (obj.event === 'delete') {
                  //在这里进行删除操作
              layer.confirm('确定要删除吗？', {
                   btn: ['确定', '取消'] //按钮
               },
                    function () {
                               $.ajax({
                                   url: '/MainData/MeasureUnitDelete',
                                   type: 'post',
                                   data: {
                                     id: data.id
                                  },
                                   success: function (res) {
                                       if (res.msg == '删除成功') {
                                           layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                                               location.reload(); //刷新页面
                                           });
                                       } else {
                                           layer.msg(res.msg, { icon: 2, time: 1000 });
                                       }
                                   }
                               });
                    }, function () {
                     layer.msg('已取消', { icon: 1, time: 1000 });
                    });
              }
              if(obj.event === 'upd'){
                //iframe层
                     layer.open({
                         type: 2,
                         title: '修改计量单位',
                         shadeClose: true,
                         shade: 0.5,
                         area: ['600px', '60%'],
                         content: '/MainData/UpdateMeasureUnitView?id='+ data.id, //iframe的src
                         end: function () {
                             // iframe层关闭后刷新页面
                             location.reload(); // 刷新页面
                         }
                     });
              }
         
          });
     });



     //添加
     function addInfor() {
          //iframe层
          layer.open({
              type: 2,
              title: '添加计量单位',
              shadeClose: true,
              shade: 0.5,
              area: ['600px', '60%'],
              content: '/MainData/MeasureUnitAddView', //iframe的src
              end: function () {
                  // iframe层关闭后刷新页面
                  location.reload(); // 刷新页面
              }
          });
     }




</script>

